﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Train_SelEmployees : System.Web.UI.Page
{
    System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitEmp();
        }
    }

    private void InitEmp()
    {
        if (Request["Multi"] != null)
        {
            litTitle.Text = "人员添加";

            btnAdd.Visible = true;
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
            btnAdd.Visible = false;

            litTitle.Text = "添加培训人员";
            string empid = "";
            string empname = "";

            string IssId = Request.Params["IssId"];
            string pIndex = Request.Params["pIndex"];
            this.Label1.Text = IssId;
            this.Label2.Text = pIndex;

            object[] obj = new object[2];
            obj[0] = "0";
            obj[1] = "=1";
            object[] myresult = Srvtools.CliUtils.CallMethod("Train", "GetAllEmps", obj);
            if (myresult != null && (int)myresult[0] == 0)
            {
                DataTable dtEmps = (DataTable)myresult[1];
                foreach (DataRow dr in dtEmps.Rows)
                {
                    nvc.Add(dr["EmployeeID"].ToString(), dr["EmployeeID"].ToString() + "(" + dr["EmpName"].ToString() + ")");
                }
            }

            object[] objs = new object[1];
            objs[0] = IssId;
 
            object[] result = Srvtools.CliUtils.CallMethod("Train", "GetEmpIdByIssId", objs);
            if (result != null && (int)result[0] == 0)
            {
                DataTable dt = (DataTable)result[1];
                Session["selected"] = dt;
                foreach (DataRow dr in dt.Rows)
                {
                    empid = dr["EmployeeID"].ToString();
                    empname = dr["EmpName"].ToString();
                    ListItem lt = new ListItem(empid + "(" + empname + ")", empid + ",已选");
                    this.lbSelect.Items.Add(lt);
                    nvc.Remove(empid);
                }

                for (int i = 0; i < nvc.Count; i++)
                {
                    ListItem li = new ListItem(nvc[i], nvc.Keys[i] + ",未选");
                    this.lbNoSelect.Items.Add(li);

                }
            }
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        this.lbNoSelect.Items.Clear();
        string depId = this.ddlDepartment.SelectDepmanetId;

        object[] obj = new object[3];
        obj[0] = depId;
        obj[2] = txtName.Text.Trim();
        if (Request["Multi"] != null)
        {
            obj[1] = "<>-1";
        }
        else
        {
            obj[1] = "=1";
        }
        object[] myresult = Srvtools.CliUtils.CallMethod("Train", "GetAllEmps", obj);
        if (myresult != null && (int)myresult[0] == 0)
        {
            DataTable dtEmps = (DataTable)myresult[1];
            nvc.Clear();
            foreach (DataRow dr in dtEmps.Rows)
            {
                nvc.Add(dr["EmployeeID"].ToString(), dr["EmployeeID"].ToString() + "(" + dr["EmpName"].ToString() + ")");
            }
        }

        foreach (ListItem li in this.lbSelect.Items)
        {
            for (int i = 0; i < nvc.Count; i++)
            {
                if (nvc.Keys[i] == li.Value)
                    nvc.Remove(nvc.Keys[i]);
            }
        }

        for (int i = 0; i < nvc.Count; i++)
        {
            ListItem li = new ListItem(nvc[i], nvc.Keys[i] + ",未选");
            this.lbNoSelect.Items.Add(li);
        }

    }

    protected void btnSelectAll_Click(object sender, EventArgs e)
    {
        bool flag = false;

        for (int i = 0; i < this.lbNoSelect.Items.Count; i++)
        {
            for (int j = 0; j < lbSelect.Items.Count; j++)
            {
                if (lbSelect.Items[j].Value == lbNoSelect.Items[i].Value)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                this.lbSelect.Items.Add(this.lbNoSelect.Items[i]);
            }

        }
        this.lbNoSelect.Items.Clear();

    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        bool flag = false;
        for (int i = lbNoSelect.Items.Count - 1; i >= 0; i--)
        {
            if (this.lbNoSelect.Items[i].Selected == true)
            {
                for (int j = 0; j < lbSelect.Items.Count; j++)
                {
                    if (lbSelect.Items[j].Value == lbNoSelect.Items[i].Value)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    this.lbSelect.Items.Add(this.lbNoSelect.Items[i]);
                    this.lbNoSelect.Items.Remove(this.lbNoSelect.Items[i]);
                }
            }


        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        for (int i = lbSelect.Items.Count - 1; i >= 0; i--)
        {
            if (this.lbSelect.Items[i].Selected == true)
            {
                this.lbNoSelect.Items.Add(this.lbSelect.Items[i]);
                this.lbSelect.Items.Remove(this.lbSelect.Items[i]);
            }
        }
    }

    protected void btnCancelAll_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.lbSelect.Items.Count; i++)
        {
            this.lbNoSelect.Items.Add(this.lbSelect.Items[i]);
        }
        this.lbSelect.Items.Clear();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataTable selecteddt = (DataTable)Session["selected"];
        int max = 0;
        if (selecteddt.Compute("max(PId)", "") != DBNull.Value)
            max = Convert.ToInt32(selecteddt.Compute("max(PId)", ""));

        DataTable dt = new DataTable();
        dt.Columns.Add("PId");
        dt.Columns.Add("IssueId");
        dt.Columns.Add("EmployeeID");

        foreach (ListItem li in this.lbSelect.Items)
        {
            if (li.Value.Split(',').GetValue(1).ToString() == "未选")
            {
                DataRow dr = dt.NewRow();
                dr["PId"] = ++max;
                dr["IssueId"] = this.Label1.Text;
                dr["EmployeeID"] = li.Value.Split(',').GetValue(0).ToString();
                dt.Rows.Add(dr);
            }
        }

        if (dt.Rows.Count <= 0)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('没有添加新的培训人员')", true);
            return;
        }

        object[] objs = new object[1];
        objs[0] = dt;
        object[] myresult = Srvtools.CliUtils.CallMethod("Train", "ExecTrain_Practice", objs);
        if (myresult != null && (int)myresult[0] == 0)
        {
            int temp = (int)myresult[1];

            if (temp > 0)
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "sucess(" + this.Label2.Text + ")", true);
            else
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('添加人员失败！');", true);
        }

    }

    public string pass()
    {
        string txt = "";
        for (int j = 0; j < this.lbSelect.Items.Count; j++)
        {

            txt += this.lbSelect.Items[j].Text + "?" + lbSelect.Items[j].Value.Substring(0,lbSelect.Items[j].Value.IndexOf(',')) + "";
            txt += ",";

        }

        if (txt.Length > 0)
        {
            return txt.Substring(0, txt.Length - 1);
        }
        else
        {
            return "";
        }

    }
}
